//
//  statsViewController.m
//  finalProject
//
//  Created by iD Student on 7/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "statsViewController.h"

@interface statsViewController ()

@end

@implementation statsViewController
@synthesize labelTitle;
@synthesize damage;
@synthesize sellPrice;
@synthesize special;
@synthesize towerImage;
@synthesize projectileTargetLeft;
@synthesize projectileTargetRight;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    leftActive = FALSE;
    rightActive = FALSE;
    leftX = 140;
    leftY = 220;
    rightX = 150;
    rightY = 220;
    if (towerType==@"lightning") {
        labelTitle.text = @"Lightning Tower";
        damage.text = @"3";
        sellPrice.text = @"50 gold";
        special.text = @" ";
        projectileSpeed = 5;
        towerImage.image = [UIImage imageNamed:@"lightningTower.png"];
        projectileImage = [UIImage imageNamed:@"lightningProjectile.png"];
    } else if (towerType==@"water") {
        labelTitle.text = @"Water Tower";
        damage.text = @"5";
        sellPrice.text = @"50 gold";
        special.text = @"Slows enemies.";
        projectileSpeed = 4;
        towerImage.image = [UIImage imageNamed:@"waterTower.png"];
        projectileImage = [UIImage imageNamed:@"waterProjectile.png"];
    } else if (towerType==@"earth") {
        labelTitle.text = @"Earth Tower";
        damage.text = @"7";
        sellPrice.text = @"50 gold";
//        special.text = @"Deals AoE damage.";
        projectileSpeed = 5;
        towerImage.image = [UIImage imageNamed:@"earthTower.png"];
        projectileImage = [UIImage imageNamed:@"earthProjectile.png"];
    } else if (towerType==@"air") {
        labelTitle.text = @"Air Tower";
        damage.text = @"2";
        sellPrice.text = @"50 gold";
//        special.text = @"Fast attack speed.";
        projectileSpeed = 8;
        towerImage.image = [UIImage imageNamed:@"airTower.png"];
        projectileImage = [UIImage imageNamed:@"airProjectile.png"];
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animate) userInfo:nil repeats:YES];
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setLabelTitle:nil];
    [self setDamage:nil];
    [self setSellPrice:nil];
    [self setSpecial:nil];
    [self setTowerImage:nil];
    [self setProjectileTargetLeft:nil];
    [self setProjectileTargetRight:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)animate {
    if (leftActive==FALSE) {
        leftActive = TRUE;
        projectileLeft = [[UIImageView alloc] initWithImage:projectileImage];
        projectileLeft.frame = CGRectMake(leftX, leftY, 30, 30);
        [self.view addSubview:projectileLeft];
    } else {
        leftX -= projectileSpeed;
        projectileLeft.center = CGPointMake(leftX, leftY);
        if (CGRectIntersectsRect(projectileLeft.frame, projectileTargetLeft.frame)) {
            projectileLeft.hidden = TRUE;
            leftX = 140;
            leftActive = FALSE;
        }
    }
    if (rightActive==FALSE) {
        rightActive = TRUE;
        projectileRight = [[UIImageView alloc] initWithImage:projectileImage];
        projectileRight.frame = CGRectMake(rightX, rightY, 30, 30);
        [self.view addSubview:projectileRight];
    } else {
        rightX += projectileSpeed;
        projectileRight.center = CGPointMake(rightX, rightY);
        if (CGRectIntersectsRect(projectileRight.frame, projectileTargetRight.frame)) {
            projectileRight.hidden = TRUE;
            rightX = 150;
            rightActive = FALSE;
        }
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)back:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}
@end
